<template>
  <div class="service">
    <div class="register">
      <el-row>
        <el-button type="primary" class="dakai" @click="createData()">提交</el-button>
      </el-row>
    </div>
    <div style="width:100%;">
      <el-form :model="CustomServiceSet" label-position="right" label-width="195px" ref="dataForm">
        <el-form-item label="客服功能">
          <el-radio-group v-model="CustomServiceSet.status">
            <el-radio label="1">开启</el-radio>
            <el-radio label="0">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="浮动条颜色">
          <el-radio-group v-model="CustomServiceSet.colour">
            <el-radio label="1" style="color:green">
              绿色
            </el-radio>
            <el-radio label="0" style="color:orange">
              橙色
            </el-radio>
            <el-radio label="2" style="color:red">
              红色
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <div class="qq">QQ客服设置</div>
        <div style="width:100%;margin-left:15px;padding-left:174px;padding-right:387px;">
          <el-radio-group v-for="(item,index) in qqCustomServiceArr" :key="index">
            <el-input v-model="item.name" placeholder="qq客服名称" style="width:130px;margin-top:10px;margin-right:10px;margin-bottom:10px;"></el-input>
            <el-input v-model="item.qq" placeholder="qq号" style="width:130px;margin-right:10px;margin-bottom:10px;"></el-input>
            <el-input v-model="item.memo" style="width:200px;margin-bottom:10px;"></el-input>
            <el-button style="color:red;margin-left:5px;" @click="diyiShan(item.id)"><i class="el-icon-minus"></i></el-button>
          </el-radio-group>
          <el-radio-group style="width:200px;height:49px;padding-top:6px;">
            <el-button type="primary" @click="handleClickAdd"><i class="el-icon-plus"></i></el-button>
          </el-radio-group>
        </div>

        <div class="qqqun">QQ群设置</div>
        <div style="width:100%;margin-left:15px;padding-left:174px;padding-right:387px;">
          <el-radio-group v-for="(item,index) in qqGroupArr" :key="index">
            <el-input v-model="item.qqGroupName" placeholder="qq群名称" style="width:130px;margin-top:10px;margin-right:10px;margin-bottom:10px;"></el-input>
            <el-input v-model="item.qqGroupNo" placeholder="qq群号" style="width:130px;margin-right:10px;margin-bottom:10px;"></el-input>
            <el-input v-model="item.memo" placeholder="qq群直接加入网页代码(选填)" style="width:200px;margin-bottom:10px;"></el-input>
            <el-button style="color:red;margin-left:5px;" @click="dierShan(item.id)"><i class="el-icon-minus"></i></el-button>
          </el-radio-group>
          <el-radio-group style="width:200px;height: 52px;padding-top:6px;">
            <el-button type="primary" @click="qunAdd"><i class="el-icon-plus"></i></el-button>
          </el-radio-group>
        </div>

        <div class="qqiphone">联系电话设置</div>
        <div style="width:100%;padding-left:188px;padding-right:502px;">
          <el-time-picker
            is-range
            v-model="CustomServiceSet.operateTimeArr"
            style="width:400px;margin-top:10px;"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围">
          </el-time-picker>
          <el-radio-group v-for="(item,index) in contactPhoneArr" :key="index">
            <el-input v-model="item.contact" placeholder="联系人" style="width:150px;margin-top:10px;margin-right:10px;margin-bottom:10px;"></el-input>
            <el-input v-model="item.phone" placeholder="联系电话" style="width:238px;margin-top:10px;margin-bottom:10px;"></el-input>
            <el-button style="color:red;margin-left:5px;" @click="disaShan(item.id)"><i class="el-icon-minus"></i></el-button>
          </el-radio-group>
          <el-radio-group style="width:200px;height:52px;padding-top:7px;">
            <el-button type="primary" @click="nb"><i class="el-icon-plus"></i></el-button>
          </el-radio-group>
        </div>


        <div class="set">其他设置</div>
        <el-form-item label="微信二维码" style="margin-top:10px;">
          <img v-if="CustomServiceSet.qrCodeUrl" :src="CustomServiceSet.qrCodeUrl" alt class="img" style="width:180px;height:180px;">
          <el-upload
            :on-change="handleLogoChange"
            :http-request="uploadLogoFile"
            :limit="1"
            action=""
            class="upload-demo"
            accept="image/jpg, image/png"
          >
            <el-button slot="trigger" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {addCustomServiceSet, deleteContactPhone, deleteQqCustomService, deleteQqGroup, getCustomServiceSet} from '@/api/system/custom_service_set';
  import {getTenantId} from '@/utils/auth';
  import {upLoadFile} from '@/api/upLoadFile'; // 数据接口

  export default {
    data() {
      return {
        CustomServiceSet: {
          status: '',
          colour: '',
          qrCodeUrl: '',
          operateTimeArr: '',
          tenantId: '',
          qqCustomService: [],
          qqGroup: [],
          contactPhone: []
        },
        qqCustomServiceArr: [{
          name: "",
          qq: "",
          memo: "",
        }],
        qqGroupArr: [{
          qqGroupName: "",
          qqGroupNo: "",
          memo: "",
        }],
        contactPhoneArr: [{
          contact: "",
          phone: ""
        }],
        logofile: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getCustomServiceSet(getTenantId()).then(response => {
          this.CustomServiceSet = response.data;
          if (this.CustomServiceSet && this.CustomServiceSet.qqCustomService) {
            this.qqCustomServiceArr = this.CustomServiceSet.qqCustomService;
          }
          if (this.CustomServiceSet && this.CustomServiceSet.qqGroup) {
            this.qqGroupArr = this.CustomServiceSet.qqGroup;
          }
          if (this.CustomServiceSet && this.CustomServiceSet.contactPhone) {
            this.contactPhoneArr = this.CustomServiceSet.contactPhone;
          }
        }).catch(err => {
          console.log(err)
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.CustomServiceSet.qqCustomService = this.qqCustomServiceArr;
            this.CustomServiceSet.qqGroup = this.qqGroupArr;
            this.CustomServiceSet.contactPhone = this.contactPhoneArr;
            this.CustomServiceSet.tenantId = getTenantId();
            addCustomServiceSet(this.CustomServiceSet).then(() => {
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
              this.fetchData();
            })
          }
        })
      },
      handleLogoChange(file) {
        this.logofile = file.raw
      },
      uploadLogoFile() {
        const fileForm = new FormData()
        fileForm.append('name', this.logofile.name)
        fileForm.append('uid', this.logofile.uid)
        fileForm.append('file', this.logofile)
        upLoadFile(fileForm).then(response => {
          this.CustomServiceSet.qrCodeUrl = response.data.url
        })
      },
      handleClickAdd() {
        this.qqCustomServiceArr.push({})
      },
      diyiShan(id) {
        deleteQqCustomService(id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.fetchData();
        })
        this.qqCustomServiceArr.splice(1, 1)
      },
      qunAdd() {
        this.qqGroupArr.push({})
      },
      dierShan(id) {
        deleteQqGroup(id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.fetchData();
        })
        this.qqGroupArr.splice(1, 1)
      },
      nb() {
        this.contactPhoneArr.push({})
      },
      disaShan(id) {
        deleteContactPhone(id).then(() => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.fetchData();
        })
        this.contactPhoneArr.splice(1, 1)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .service {
    .submmit {
      width: 100%;
      height: 70px;
      line-height: 70px;
      background-color: #e6eaee;
      padding-right: 30px;
      padding-top: 17px;
    }

    .headbutton {
      .el-row {
        margin-top: 30px;

        span {
          float: right;
          font-size: 18px;

        }
      }
    }

    section {
      display: block;
      width: 100%;
      height: 40px;
      line-height: 40px;
      background-color: #f0f3ff;
      margin-top: 20px;
      padding-left: 20px;
    }
  }

  .register {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-left: 15px;
    padding-left: 10px;
    background-color: #e7e9ed;
    padding-top: 15px;
    padding-right: 34px;
  }

  .dakai {
    float: right;
    position: absolute;
    right: 10px;
    bottom: -25px;
  }

  .qq {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-left: 15px;
    padding-left: 10px;
    background-color: #f0f3ff;
    padding-right: 34px;
  }

  .qqqun {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-left: 15px;
    padding-left: 10px;
    background-color: #ebf4ff;
    padding-right: 34px;
  }

  .qqiphone {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-left: 15px;
    padding-left: 10px;
    background-color: #e4fafd;
    padding-right: 34px;
  }

  .set {
    width: 100%;
    height: 44px;
    line-height: 44px;
    margin-left: 15px;
    padding-left: 10px;
    background-color: #ecfbe9;
    padding-right: 34px;
  }
</style>
